<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>
        宠物添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/statics/css/main.css" media="all">
    <script type="text/javascript" src="${pageContext.request.contextPath }/statics/lib/loading/okLoading.js"></script>
    <style>
        .imgs {
            display: none;
        }

        .picture {
            display: none;
        }
    </style>

</head>
<body>

<div class="x-body">
    <form class="layui-form layui-form-pane" action="" id="add" method="post">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <input type="hidden" name="id" value="${param.id}">
            <input type="hidden" name="personnel_id" id="personnel_id" value="">
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>主人姓名
                        </label>
                        <div class="layui-input-block">
                            <select name="personnelId" id="personnelId" lay-filter="personnelName">
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>宠物名称
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="petName" autocomplete="off" class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>宠物颜色
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="petColor" autocomplete="off" class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>收养时间
                        </label>

                        <div class="layui-input-inline">
                            <input class="layui-input" name="adopDate" placeholder="收养时间" id="LAY_demorange_s" value="">
                        </div>

                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class="x-red">*</span>宠物缩略图
                        </label>
                        <div class="layui-input-inline">
                            <div class="site-demo-upbar">
                                <div class=" layui-upload-button" style="border:#FFFFFF ;">
                                    <button type="button" class="layui-btn" id="test1">
                                        <i class="layui-icon">&#xe67c;</i>上传图片
                                    </button>
                                    <input class="layui-upload" type="file" accept="undefined" id="pet_img"
                                           name="pet_img" onchange="upload(this)">
                                </div>
                            </div>
                        </div>
                        <a href="javascript:;" style="" class="layui-btn " id="cancel"><i
                                class="layui-icon">ဂ</i>撤销上传</a>
                    </div>
                    <div class="layui-form-item imgs" id="imgshow">
                        <label class="layui-form-label">宠物缩略图展示
                        </label>
                        <img src="" id="pimages" name="pimages" style="width: 400px;height: 200px;"/>
                        <input id="avatar" name="petImg" required="" type="hidden" value="">
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>品种
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="variety" autocomplete="off" class="layui-input" value="">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <button class="layui-btn" type="button" lay-filter="add" lay-submit="">
                            保存
                        </button>
                        <button class="layui-btn" type="reset">
                            重置
                        </button>
                    </div>
                    <div style="height:100px;"></div>
                </div>
            </div>
        </div>
    </form>
</div>
<script src="${pageContext.request.contextPath }/statics/lib/layui/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/statics/js/x-layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/statics/js/jquery.min.js" charset="UTF-8"></script>
<script>
    if ($("input[name='id']").val() != "") {
        var id = $("input[name='id']").val()
        $.get("/pet/" + id, function (data) {
            $("input[name='personnel_id']").val(data.data.personnelId);
            $("input[name='petName']").val(data.data.petName);
            $("input[name='petColor']").val(data.data.petColor);
            $("input[name='adopDate']").val(data.data.adopDate);
            if (data.data.petImg != "" && data.data.petImg != null) {
                $("#pimages").attr('src', data.data.petImg);
                $("#avatar").val(data.data.petImg);
                $(".imgs").show();
            }
            $("input[name='variety']").val(data.data.variety);
        })
    }
    //加载人员列表
    $(function getPersonnelList() {
        $.ajax({
            type: "get",
            url: "/personnel/getlist",
            dataType: "json",
            success: function (data) {
                $("#personnelName").html("");
                var personnel_id = $("input[name='personnel_id']").val()
                var str = "<option value='0'>--请选择所属主人--</option>"
                for (var i = 0; i < data.data.length; i++) {
                    if (data.data[i].id == personnel_id) {
                        str += "<option selected='selected' value=" + data.data[i].id + ">" + data.data[i].memberName + "</option>"
                    } else {
                        str += "<option value=" + data.data[i].id + ">" + data.data[i].memberName + "</option>"
                    }
                }
                $("#personnelId").append(str);

            }, error: function (data) {//当访问时候，404，500 等非200的错误状态码
                alert("加载所属人员列表失败！");
            }

        })
    })
    layui.use(['laydate', 'element', 'layer', 'form'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        lement = layui.element();//面包导航
        layer = layui.layer;//弹出层
        form = layui.form();
        okLoading.close($);
        form.render();

        var start = {
            max: '2027-1-1'
            , choose: function (datas) {
            }
        };

        document.getElementById('LAY_demorange_s').onclick = function () {
            start.elem = this;
            laydate(start);
        };

        //监听提交
        form.on('submit(add)', function (data) {
            var id = $("input[name='id']").val();
            var personnelId = $("#personnelId option:selected").val();
            var petName = $("input[name='petName']").val();
            var petColor = $("input[name='petColor']").val();
            var adopDate = $("input[name='adopDate']").val();
            var img = $("input[name='pet_img']").val();
            var variety = $("input[name='variety']").val();
            if (personnelId == "0") {
                layer.msg('请重新选择主人姓名', {icon: 5, time: 2000});
                return false;
            }
            if (petName == "") {
                layer.msg('宠物名不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (petColor == "") {
                layer.msg("宠物颜色不能为空！", {icon: 5, time: 2000});
                return false;
            }
            if (adopDate == "") {
                layer.msg("领养时间不能为空！", {icon: 5, time: 2000});
                return false;
            }
            if (variety == "") {
                layer.msg("宠物品种！", {icon: 5, time: 2000});
                return false;
            }
            // $("input[name='personnel_id']").val(document.getElementById("personnelId").value)
            // var personnelId=$("input[name='personnelId']").val();
            var pet = data.field;
            console.info(pet)
            if (id != "") {
                $.ajax({
                    type: "post",
                    url: "update",
                    data: pet,
                    dataType: "json",
                    success: function (data) {
                        if (data.data == true) {
                            layer.msg("更新成功", {icon: 6, time: 1000}, function () {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                            return false;

                        } else {
                            layer.msg("更新失败", {icon: 5, time: 1000});
                            return false;
                        }
                    }
                });
            }
            if (id == "") {      //添加
                $.ajax({
                    type: "post",
                    url: "add",
                    data: pet,
                    dataType: "json",
                    success: function (data) {
                        if (data.data == true) {
                            layer.msg("添加成功", {icon: 6, time: 2000}, function () {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                            return false;
                        }
                        else {
                            layer.msg("添加失败", {icon: 5, time: 2000});
                            return false;
                        }
                    }
                });
            }

        });
    })
</script>
<!--宠物缩略图上传-->
<script>
    function upload(obj) {
        var formData = new FormData();
        formData.append('images', $('#pet_img')[0].files[0]);
        console.log(formData)
        layer.msg('图片上传中', {icon: 16});
        $.ajax({
            type: "post",
            async: true,
            processData: false,
            contentType: false,
            url: "/upload/uploadFile",
            data: formData,
            success: function (data) {
                console.log(data)
                if (data.msg == '执行成功') {
                    console.log(data.data);
                    layer.closeAll('loading');
                    $("#pimages").attr('src', data.data);
                    $("#avatar").val(data.data);
                    $(".imgs").show();
                    return false;
                } else {
                    layer.msg("操作失败", {icon: 2, time: 1000});
                }
            }
        });
    }
</script>

</body>
</html>